#include <stdio.h>
#include <stdlib.h>

// 定义抽象接口：图形操作函数指针
typedef struct Shape {
    void (*draw)(void);
    void (*area)(void);
} Shape;

// 具体图形：圆形
void circle_draw() { printf("Drawing Circle\n"); }
void circle_area() { printf("Area of Circle: πr²\n"); }
Shape* create_circle() {
    Shape* circle = (Shape*)malloc(sizeof(Shape));
    circle->draw = circle_draw;
    circle->area = circle_area;
    return circle;
}

// 具体图形：矩形
void rectangle_draw() { printf("Drawing Rectangle\n"); }
void rectangle_area() { printf("Area of Rectangle: w*h\n"); }
Shape* create_rectangle() {
    Shape* rect = (Shape*)malloc(sizeof(Shape));
    rect->draw = rectangle_draw;
    rect->area = rectangle_area;
    return rect;
}

// 工厂函数：根据类型创建对象
Shape* shape_factory(const char* type) {
    if (strcmp(type, "circle") == 0) {
        return create_circle();
    } else if (strcmp(type, "rectangle") == 0) {
        return create_rectangle();
    }
    return NULL;
}

int main() {
    // 通过工厂创建对象
    Shape* obj1 = shape_factory("circle");
    Shape* obj2 = shape_factory("rectangle");
    
    if (obj1) {
        obj1->draw();  // Output: Drawing Circle
        obj1->area();  // Output: Area of Circle: πr²
        free(obj1);
    }
    
    if (obj2) {
        obj2->draw();  // Output: Drawing Rectangle
        obj2->area();  // Output: Area of Rectangle: w*h
        free(obj2);
    }
    
    return 0;
}